package com.lun.easy;

public class BinaryNumberWithAlternatingBits {

	// 方法一：我写的
	public boolean hasAlternatingBits(int n) {
		int first = n & 1, second;
		n >>= 1;
		while (n != 0) {
			second = n & 1;
			if (first == second)
				return false;
			first = second;
			n >>= 1;
		}
		return true;
	}

	// 方法二：别人写
	public boolean hasAlternatingBits2(int n) {
		n = n ^ (n >> 1);
		return (n & n + 1) == 0;
	}

}
